<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Member extends CI_Controller {

    public $render_data;

    function __construct() {
        parent::__construct();

        $this->load->library('template');
        $this->template->set_template('backend');
        $this->load->library('carabiner');

        $this->load->model('member_model');
        /*
         * check admin and return session data
         */
        if (!is_loggedin()) {
            redirect($this->router->reverseRoute('backend_login') . '?redirect=' . base64_encode(current_url()));
        }
    }

    /**
     * List member
     */
    function index($role = '') {
        $content_data['role'] = $role;

        $this->template->write_view('side_menu', 'backend/side_menu');
        $this->template->write_view('content', 'backend/member/member_list', $content_data);
        $this->template->render();
    }

    /**
     * Create member
     */
    function create() {
        if (!is_admin()) {
            echo 'Permission denied';
            exit;
        }

        if ($_POST) {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[3]|max_length[15]|is_unique[members.member_username]');
            $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[members.member_email]');
            $this->form_validation->set_rules('firstname', 'First name', 'trim|required');
            $this->form_validation->set_rules('lastname', 'Last name', 'trim|required');
            $this->form_validation->set_rules('password', 'Password', 'trim|required');
            $this->form_validation->set_rules('gender', 'Gender', 'trim|required');
            $this->form_validation->set_rules('birthday', 'Birth day', 'trim|required');
            $this->form_validation->set_rules('contact', 'Phone', 'trim|required');
            $this->form_validation->set_rules('address', 'Address', 'trim|required');
            $this->form_validation->set_rules('role', 'Role', 'trim|required');
            if ($this->form_validation->run() === TRUE) {
                $contact = preg_replace('#[^0-9]#', '', $this->input->post('contact'));
                $create_data = array(
                    'member_username' => $this->input->post('username'),
                    'member_email' => $this->input->post('email'),
                    'member_firstname' => $this->input->post('firstname'),
                    'member_lastname' => $this->input->post('lastname'),
                    'member_password' => $this->input->post('password'),
                    'member_address' => $this->input->post('address'),
                    'member_contact' => $contact,
                    'member_birthday' => $this->input->post('birthday'),
                    'member_gender' => $this->input->post('gender'),
                    'member_role' => $this->input->post('role')
                );

                $insert_id = $this->member_model->member_create($create_data);
                if ($insert_id) {
                    if (isset($_FILES['thumb']['name']) && $_FILES['thumb']['name'] != "") {

                        $thumb_data = upload_thumb($_FILES['thumb']);
                        $this->member_model->add_thumb($insert_id, $thumb_data);
                    }
                    redirect('backend/member/index/' . $this->input->post('role'));
                }
            } else {
                echo validation_errors();
                exit;
            }
        } else {
            $this->load->helper('form');
            $this->template->write_view('side_menu', 'backend/side_menu');
            $this->template->write_view('content', 'backend/member/member_create');
            $this->template->render();
            return TRUE;
        }
    }

    /**
     * Edit member
     */
    function edit($member_id = 0) {

        if (!is_admin()) {
            echo 'Permission denied';
            exit;
        }

        if ($_POST) {

            $this->load->library('form_validation');
            $this->form_validation->set_rules('firstname', 'First name', 'trim|required');
            $this->form_validation->set_rules('lastname', 'Last name', 'trim|required');
            $this->form_validation->set_rules('gender', 'Gender', 'trim|required');
            $this->form_validation->set_rules('birthday', 'Birth day', 'trim|required');
            $this->form_validation->set_rules('contact', 'Phone', 'trim|required');
            $this->form_validation->set_rules('address', 'Address', 'trim|required');
            $this->form_validation->set_rules('role', 'Role', 'trim|required');
            if ($this->form_validation->run() === TRUE) {
                $contact = preg_replace('#[^0-9]#', '', $this->input->post('contact'));
                $edit_data = array(
                    'member_firstname' => $this->input->post('firstname'),
                    'member_lastname' => $this->input->post('lastname'),
                    'member_address' => $this->input->post('address'),
                    'member_password' => $this->input->post('password'),
                    'member_contact' => $contact,
                    'member_birthday' => $this->input->post('birthday'),
                    'member_gender' => $this->input->post('gender'),
                    'member_role' => $this->input->post('role')
                );

                $result = $this->member_model->member_edit($member_id, $edit_data);

                if ($result) {

                    if (isset($_FILES['thumb']['name']) && $_FILES['thumb']['name'] != "") {

                        $thumb_data = upload_thumb($_FILES['thumb']);
                        $this->member_model->add_thumb($member_id, $thumb_data);
                    }

                    redirect('backend/member/index/' . $this->input->post('role'));
                }
            } else {
                
            }
        } else {
            if (!$member_id && is_loggedin()) {
                $member = $this->session->userdata('is_loggedin');
                redirect('backend/member/edit/' . $member['member_id']);
            }

            if (!$member_id) {
                redirect($this->router->reverseRoute('backend_member'));
            }
        }

        $member_data = $this->member_model->member_get($member_id);
        //var_dump($member_data);

        $this->load->helper('form');
        $this->template->write_view('side_menu', 'backend/side_menu');
        $this->template->write_view('content', 'backend/member/member_edit', $member_data);
        $this->template->render();
    }

    function delete($id) {
        if (!is_admin()) {
            echo 'Permission denied';
            exit;
        }

        $this->member_model->delete($id);
        return true;
    }

    /**
     * Change status 
     */
    function status($member_id, $member_status) {
        $member_id = floor($member_id);
        $member_status = floor($member_status);
        if ($member_id == 0 || !isset($member_status)) {
            redirect($this->router->reverseRoute('backend_member'));
        }

        $result = $this->backend_member->member_status($member_id, $member_status);
        if ($result) {
            redirect($this->router->reverseRoute('backend_member'));
        }
    }

    function check_email() {
        if ($_POST) {
            $email = $this->input->post('email');
            if ($this->member_model->check_email($email)) {
                echo TRUE;
            } else {
                echo '{"message":"This Email is already in used."}';
            }
            return FALSE;
        }
    }

    function check_username() {
        if ($_POST) {
            $username = $this->input->post('username');
            if ($this->member_model->check_username($username)) {
                echo TRUE;
            } else {
                echo '{"message":"This username is already in used."}';
            }
            return FALSE;
        }
    }

    function json_data($role = '') {


        $this->load->library('Datatables');

        if (is_admin()) {
            $buttons = '<a class="btn btn-default pull-right tbl-delete-row" href="#" rel="' . base_url('backend/member/delete') . '/$1" ><i class="icon-remove"></i></a>';
            $buttons .= '<a class="btn btn-default pull-right" href="' . base_url('backend/member/edit') . '/$1" rel="$1"  ><i class="icon-edit"></i></a>';
        }else{
            $buttons = '<a class="btn btn-default pull-right tbl-delete-row disable" href="#" ><i class="icon-remove"></i></a>';
            $buttons .= '<a class="btn btn-default pull-right disable" href="#" rel="$1"  ><i class="icon-edit"></i></a>';
        }
        
        
        $name = '$1 $2';
        $this->datatables
                ->select('members.member_id,members.member_username,members.member_firstname,members.member_lastname,members.member_contact,members.member_email')
                ->from('members')
                ->edit_column('members.member_firstname', $name, 'members.member_firstname,members.member_lastname')
                ->unset_column('members.member_lastname')
                ->add_column('Tools', $buttons, 'members.member_id');

        if ($role != '')
            $this->datatables->where('members.member_role', $role);

        $data['result'] = $this->datatables->generate();
        echo $data['result'];
    }

}
